package until;

import io.appium.java_client.android.AndroidDriver;
import org.aspectj.util.FileUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebElement;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Description
 * @auther lileilei
 * @create 2020-02-29 17:20
 */
public class CommonUntilAppWebDrivers {

    /**
     * 封装driver操作，默认返回null，这样执行的click操作
     * 如果返回的不是空，则返回的是断言的，
     *
     * @param driver
     * @param elements
     * @param index
     * @param type
     * @param typeelements
     * @return
     */
    public String opearElementsInApp(AndroidDriver driver, String elements,
                                     Integer index, String type,
                                     String typeelements,
                                     String sendkey, String opearname) {
        if (type.equals("id")) {
            List<WebElement> webElements = driver.findElements(By.id(elements));
            if (typeelements.equals("text")) {
                String reslut = webElements.get(index).getText();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return reslut;
            } else if (typeelements.equals("send")) {
                WebElement w = webElements.get(index);
                w.clear();
                w.sendKeys(sendkey);
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                webElements.get(index).click();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        } else if (type.equals("xpath")) {
            List<WebElement> webElements = driver.findElements(By.xpath(elements));
            if (typeelements.equals("text")) {
                String reslut = webElements.get(index).getText();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return reslut;
            } else if (typeelements.equals("send")) {
                WebElement w = webElements.get(index);
                w.clear();
                w.sendKeys(sendkey);
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            } else {
                webElements.get(index).click();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        } else if (type.equals("name")) {
            List<WebElement> webElements = driver.findElements(By.name(elements));
            if (typeelements.equals("text")) {
                String reslut = webElements.get(index).getText();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return reslut;
            } else if (typeelements.equals("send")) {
                WebElement w = webElements.get(index);
                w.clear();
                w.sendKeys(sendkey);
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            } else {
                webElements.get(index).click();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        } else if (type.equals("className")) {
            List<WebElement> webElements = driver.findElements(By.className(elements));
            if (typeelements.equals("text")) {
                String reslut = webElements.get(index).getText();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return reslut;
            } else if (typeelements.equals("send")) {
                WebElement w = webElements.get(index);
                w.clear();
                w.sendKeys(sendkey);
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            } else {
                webElements.get(index).click();
                return null;
            }
        } else if (type.equals("tagName")) {
            List<WebElement> webElements = driver.findElements(By.tagName(elements));
            if (typeelements.equals("text")) {
                String reslut = webElements.get(index).getText();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return reslut;
            } else if (typeelements.equals("send")) {
                WebElement w = webElements.get(index);
                w.clear();
                w.sendKeys(sendkey);
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            } else {
                webElements.get(index).click();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        } else if (type.equals("cssSelector")) {
            List<WebElement> webElements = driver.findElements(By.cssSelector(elements));
            if (typeelements.equals("text")) {
                String reslut = webElements.get(index).getText();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return reslut;
            } else if (typeelements.equals("send")) {
                WebElement w = webElements.get(index);
                w.clear();
                w.sendKeys(sendkey);
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            } else {
                webElements.get(index).click();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        } else if (type.equals("linkText")) {
            List<WebElement> webElements = driver.findElements(By.linkText(elements));
            if (typeelements.equals("text")) {
                String reslut = webElements.get(index).getText();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return reslut;
            } else if (typeelements.equals("send")) {
                WebElement w = webElements.get(index);
                w.clear();
                w.sendKeys(sendkey);
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            } else {
                webElements.get(index).click();
                File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
                String name = System.getProperty("user.dir") + File.separator + "report" + File.separator + opearname + ".jpg";
                try {
                    FileUtil.copyFile(screenShotFile, new File(name));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        } else {
            return null;
        }
        return null;
    }
}
